<!DOCTYPE html>

<html lang="en" data-content_root="../">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />

  <title>CheckPIESupported &mdash; CMake 4.1.1 Documentation</title>

    <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=a2c47e09" />
    <link rel="stylesheet" type="text/css" href="../_static/cmake.css?v=4d06bd55" />
    
    <script src="../_static/documentation_options.js?v=e6a937a4"></script>
    <script src="../_static/doctools.js?v=9bcbadda"></script>
    <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
    
    <link rel="icon" href="../_static/cmake-favicon.ico"/>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="CheckPrototypeDefinition" href="CheckPrototypeDefinition.html" />
    <link rel="prev" title="CheckOBJCXXSourceRuns" href="CheckOBJCXXSourceRuns.html" />
 

  </head><body>
    <input id="sidebar-check" type="checkbox" />
    <label id="sidebar-overlay" for="sidebar-check"></label>



    <div class="related relbar1" role="navigation" aria-label="Related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="CheckPrototypeDefinition.html" title="CheckPrototypeDefinition"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="CheckOBJCXXSourceRuns.html" title="CheckOBJCXXSourceRuns"
             accesskey="P">previous</a> |</li>
  <li>
    <label class="sidebar-toggle" for="sidebar-check"></label>
  </li>
  <li class="rootlink">
    <img src="../_static/cmake-logo-16.png" width="16" height="16" alt=""/>
    <a href="https://cmake.org/">CMake 4.1.1</a>
    <span class="reldelim1"> &#187;</span>
  </li>
  <li>
    <a href="../index.html">Documentation</a> &#187;
  </li>

          <li class="nav-item nav-item-1"><a href="../manual/cmake-modules.7.html" accesskey="U">cmake-modules(7)</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">CheckPIESupported</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="checkpiesupported">
<span id="module:CheckPIESupported"></span><h1>CheckPIESupported<a class="headerlink" href="#checkpiesupported" title="Link to this heading">¶</a></h1>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.14.</span></p>
</div>
<p>This module provides the <code class="docutils literal notranslate"><span class="pre">check_pie_supported()</span></code> function to check whether the
linker supports Position Independent Code (PIE) or No Position Independent Code
(NO_PIE) for executables.</p>
<p>When setting the <span class="target" id="index-0-prop_tgt:POSITION_INDEPENDENT_CODE"></span><a class="reference internal" href="../prop_tgt/POSITION_INDEPENDENT_CODE.html#prop_tgt:POSITION_INDEPENDENT_CODE" title="POSITION_INDEPENDENT_CODE"><code class="xref cmake cmake-prop_tgt docutils literal notranslate"><span class="pre">POSITION_INDEPENDENT_CODE</span></code></a> target property,
PIC-related compile and link options are added when building library objects,
and PIE-related compile options are added when building objects of executable
targets, regardless of this module.  Use this module to ensure that the
<code class="docutils literal notranslate"><span class="pre">POSITION_INDEPENDENT_CODE</span></code> target property for executables is also honored at
link time.</p>
<dl class="cmake command">
<dt class="sig sig-object cmake" id="command:check_pie_supported">
<span class="sig-name descname"><span class="pre">check_pie_supported</span></span><a class="headerlink" href="#command:check_pie_supported" title="Link to this definition">¶</a></dt>
<dd><div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">check_pie_supported(</span><span class="p">[</span><span class="no">OUTPUT_VARIABLE</span><span class="w"> </span><span class="nv">&lt;output&gt;</span><span class="p">]</span>
<span class="w">                    </span><span class="p">[</span><span class="no">LANGUAGES</span><span class="w"> </span><span class="nv">&lt;lang&gt;...</span><span class="p">]</span><span class="nf">)</span>
</pre></div>
</div>
<p>Options are:</p>
<dl>
<dt><code class="docutils literal notranslate"><span class="pre">OUTPUT_VARIABLE</span> <span class="pre">&lt;output&gt;</span></code></dt><dd><p>Set <code class="docutils literal notranslate"><span class="pre">&lt;output&gt;</span></code> variable with details about any error. If the check is
bypassed because it uses cached results from a previous call, the output
will be empty even if errors were present in the previous call.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">LANGUAGES</span> <span class="pre">&lt;lang&gt;...</span></code></dt><dd><p>Check the linkers used for each of the specified languages.
If this option is not provided, the command checks all enabled languages.</p>
<p><code class="docutils literal notranslate"><span class="pre">C</span></code>, <code class="docutils literal notranslate"><span class="pre">CXX</span></code>, <code class="docutils literal notranslate"><span class="pre">Fortran</span></code> are supported.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.23: </span><code class="docutils literal notranslate"><span class="pre">OBJC</span></code>, <code class="docutils literal notranslate"><span class="pre">OBJCXX</span></code>, <code class="docutils literal notranslate"><span class="pre">CUDA</span></code>, and <code class="docutils literal notranslate"><span class="pre">HIP</span></code> are supported.</p>
</div>
</dd>
</dl>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>To use <code class="docutils literal notranslate"><span class="pre">check_pie_supported()</span></code>, policy <span class="target" id="index-0-policy:CMP0083"></span><a class="reference internal" href="../policy/CMP0083.html#policy:CMP0083" title="CMP0083"><code class="xref cmake cmake-policy docutils literal notranslate"><span class="pre">CMP0083</span></code></a> must be set to
<code class="docutils literal notranslate"><span class="pre">NEW</span></code>; otherwise, a fatal error will occur.</p>
</div>
</dd></dl>

<section id="variables">
<h2>Variables<a class="headerlink" href="#variables" title="Link to this heading">¶</a></h2>
<p>For each language checked, the <code class="docutils literal notranslate"><span class="pre">check_pie_supported()</span></code> function defines two
boolean cache variables:</p>
<blockquote>
<div><dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">CMAKE_&lt;lang&gt;_LINK_PIE_SUPPORTED</span></code></dt><dd><p>Set to true if <code class="docutils literal notranslate"><span class="pre">PIE</span></code> is supported by the linker and false otherwise.</p>
</dd>
<dt><code class="docutils literal notranslate"><span class="pre">CMAKE_&lt;lang&gt;_LINK_NO_PIE_SUPPORTED</span></code></dt><dd><p>Set to true if <code class="docutils literal notranslate"><span class="pre">NO_PIE</span></code> is supported by the linker and false otherwise.</p>
</dd>
</dl>
</div></blockquote>
</section>
<section id="examples">
<h2>Examples<a class="headerlink" href="#examples" title="Link to this heading">¶</a></h2>
<p>To enable PIE on an executable target at link time as well, include this module
and call <code class="docutils literal notranslate"><span class="pre">check_pie_supported()</span></code> before setting the
<code class="docutils literal notranslate"><span class="pre">POSITION_INDEPENDENT_CODE</span></code> target property.  This will determine whether the
linker for each checked language supports PIE-related link options.  For
example:</p>
<div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">add_executable(</span><span class="nb">foo</span><span class="w"> </span><span class="p">...</span><span class="nf">)</span>

<span class="nf">include(</span><span class="nb">CheckPIESupported</span><span class="nf">)</span>
<span class="nf">check_pie_supported()</span>
<span class="nf">set_property(</span><span class="no">TARGET</span><span class="w"> </span><span class="nb">foo</span><span class="w"> </span><span class="no">PROPERTY</span><span class="w"> </span><span class="no">POSITION_INDEPENDENT_CODE</span><span class="w"> </span><span class="no">TRUE</span><span class="nf">)</span>
</pre></div>
</div>
<p>Since not all linkers require or support PIE-related link options (for example,
<code class="docutils literal notranslate"><span class="pre">MSVC</span></code>), retrieving any error messages might be useful for logging purposes:</p>
<div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">add_executable(</span><span class="nb">foo</span><span class="w"> </span><span class="p">...</span><span class="nf">)</span>

<span class="nf">include(</span><span class="nb">CheckPIESupported</span><span class="nf">)</span>
<span class="nf">check_pie_supported(</span><span class="no">OUTPUT_VARIABLE</span><span class="w"> </span><span class="nb">output</span><span class="w"> </span><span class="no">LANGUAGES</span><span class="w"> </span><span class="no">C</span><span class="nf">)</span>
<span class="nf">set_property(</span><span class="no">TARGET</span><span class="w"> </span><span class="nb">foo</span><span class="w"> </span><span class="no">PROPERTY</span><span class="w"> </span><span class="no">POSITION_INDEPENDENT_CODE</span><span class="w"> </span><span class="no">TRUE</span><span class="nf">)</span>
<span class="nf">if(</span><span class="no">NOT</span><span class="w"> </span><span class="no">CMAKE_C_LINK_PIE_SUPPORTED</span><span class="nf">)</span>
<span class="w">  </span><span class="nf">message(</span><span class="no">WARNING</span><span class="w"> </span><span class="s">&quot;PIE is not supported at link time:\n${output}&quot;</span>
<span class="w">                  </span><span class="s">&quot;PIE link options will not be passed to linker.&quot;</span><span class="nf">)</span>
<span class="nf">endif()</span>
</pre></div>
</div>
<p>Setting the <code class="docutils literal notranslate"><span class="pre">POSITION_INDEPENDENT_CODE</span></code> target property on an executable
without this module will set PIE-related compile options but not PIE-related
link options, which might not be sufficient in certain cases:</p>
<div class="highlight-cmake notranslate"><div class="highlight"><pre><span></span><span class="nf">add_executable(</span><span class="nb">foo</span><span class="w"> </span><span class="p">...</span><span class="nf">)</span>
<span class="nf">set_property(</span><span class="no">TARGET</span><span class="w"> </span><span class="nb">foo</span><span class="w"> </span><span class="no">PROPERTY</span><span class="w"> </span><span class="no">POSITION_INDEPENDENT_CODE</span><span class="w"> </span><span class="no">TRUE</span><span class="nf">)</span>
</pre></div>
</div>
</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="Main">
        <div class="sphinxsidebarwrapper">
  <div>
    <h3>Table of Contents</h3>
    <ul>
<li><a class="reference internal" href="#">CheckPIESupported</a><ul>
<li><a class="reference internal" href="#variables">Variables</a></li>
<li><a class="reference internal" href="#examples">Examples</a></li>
</ul>
</li>
</ul>

  </div>
  <div>
    <h4>Previous topic</h4>
    <p class="topless"><a href="CheckOBJCXXSourceRuns.html"
                          title="previous chapter">CheckOBJCXXSourceRuns</a></p>
  </div>
  <div>
    <h4>Next topic</h4>
    <p class="topless"><a href="CheckPrototypeDefinition.html"
                          title="next chapter">CheckPrototypeDefinition</a></p>
  </div>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/module/CheckPIESupported.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<search id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</search>
<script>document.getElementById('searchbox').style.display = "block"</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="Related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="CheckPrototypeDefinition.html" title="CheckPrototypeDefinition"
             >next</a> |</li>
        <li class="right" >
          <a href="CheckOBJCXXSourceRuns.html" title="CheckOBJCXXSourceRuns"
             >previous</a> |</li>
  <li>
    <label class="sidebar-toggle" for="sidebar-check"></label>
  </li>
  <li class="rootlink">
    <img src="../_static/cmake-logo-16.png" width="16" height="16" alt=""/>
    <a href="https://cmake.org/">CMake 4.1.1</a>
    <span class="reldelim1"> &#187;</span>
  </li>
  <li>
    <a href="../index.html">Documentation</a> &#187;
  </li>

          <li class="nav-item nav-item-1"><a href="../manual/cmake-modules.7.html" >cmake-modules(7)</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">CheckPIESupported</a></li> 
      </ul>
    </div>

    <div class="footer" role="contentinfo">
    &#169; Copyright 2000-2025 Kitware, Inc. and Contributors.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.1.3.
    </div>
<script type="text/javascript">
(function() {
  "use strict";
  const hide = () => document.getElementById("sidebar-check").checked = false;
  addEventListener("keydown", e => (e.key === "Escape") && hide());
  addEventListener("click", e => (e.target.tagName === "A") && hide());
  addEventListener("hashchange", hide)
})();
</script>
  </body>
</html>